#include "StdAfx.h"
#include "ResourceFactory.h"
#include "tinyxml.h"
#include "atlimage.h"
#ifndef TEST
const char * g_xml = [xml];
#endif

SINGLETON_IMPL(ResourceFactory)

ResourceFactory::ResourceFactory(void)
{
}

ResourceFactory::~ResourceFactory(void)
{
}
BOOL ResourceFactory::LoadFile(const CString&dir)
{
	return m_file.Open(dir,CFile::modeReadWrite|CFile::typeBinary);
}
void ResourceFactory::CloseFile()
{
	m_file.Close();
}
BOOL ResourceFactory::GetData(const char *item,
							  const char *category,
							 int number,
							 CArray<PngItem *> *img_list)
{
	TiXmlDocument doc;
#ifdef TEST
	if (!doc.LoadFile( "madeByHand.xml" )) {
		return FALSE;
	}
#else
	if (doc.Parse(g_xml))
	{
		return FALSE;
	}
#endif
	TiXmlElement * root = doc.RootElement();
	TiXmlHandle hRoot=TiXmlHandle(root);
	if (!root) {
		return FALSE;
	}
	TiXmlElement * png = root->FirstChildElement( item );
	if (!png) {
		return FALSE;
	}
	TiXmlElement * cate = png->FirstChildElement(category);
	if (!cate) {
		return FALSE;
	}
	int offset = 0;
	int size = 0;

	TiXmlElement *i = cate->FirstChildElement();
	
	for (i;i&&number>0;number--,i = i->NextSiblingElement()) {
		i->Attribute("offset",&offset);
		i->Attribute("size",&size);
		if (size <= 0) {
			continue;
		}
		BYTE* lpRsrc = new BYTE[size];
		m_file.Seek(offset,CFile::begin);
		if (m_file.Read(lpRsrc,size) == size) {

			PngItem *p =new PngItem;
			p->ptr = lpRsrc;
			p->size = size;
			img_list->Add(p);	
		} else {
			delete [] lpRsrc;
		}
		
	}

	return TRUE;
}
/*
BOOL ResourceFactory::GetPngCategory(CArray<CString>*list)
{
	TiXmlDocument doc;
#ifdef TEST
	if (!doc.LoadFile( "madeByHand.xml" )) {
		return FALSE;
	}
#else
	if (doc.Parse(g_xml))
	{
		return FALSE;
	}
#endif
	TiXmlElement * root = doc.RootElement();
	TiXmlHandle hRoot=TiXmlHandle(root);
	if (!root) {
		return FALSE;
	}
	TiXmlElement * png = root->FirstChildElement("png");
	if (!png) {
		return FALSE;
	}
	TiXmlElement *cate = png->FirstChildElement();

	CString name;
	for (cate;cate;cate = cate->NextSiblingElement()) {
		CStringA t = cate->Attribute("name");
		name = CA2W(t,CP_UTF8);
		list->Add(name);
	}
}*/
void ResourceFactory::LoadMemImage(void *pimg,void *pMemData,long len)
{
	CImage *img = (CImage *)pimg;
	HGLOBAL hGlobal  =  GlobalAlloc(GMEM_MOVEABLE, len);
	void *  pData  =  GlobalLock(hGlobal);
	memcpy(pData, pMemData, len);
	GlobalUnlock(hGlobal);

	IStream *  pStream  =  NULL;
	if  (CreateStreamOnHGlobal(hGlobal, TRUE,  & pStream)  ==  S_OK)
	{

		if (SUCCEEDED(img->Load(pStream)))
		{
			for(int i = 0; i < img->GetWidth(); i++)  
			{  
				for(int j = 0; j < img->GetHeight(); j++)  
				{  
					unsigned char* pucColor = reinterpret_cast<unsigned char *>(img->GetPixelAddress(i , j));  
					pucColor[0] = pucColor[0] * pucColor[3] / 255;  
					pucColor[1] = pucColor[1] * pucColor[3] / 255;  
					pucColor[2] = pucColor[2] * pucColor[3] / 255;  
				}  
			} 

		} 
		pStream -> Release();     
	} 
	GlobalFree(hGlobal);

} 